<template>
  <div>
    <el-button type="primary" size="default" @click="handleOutput"
      >Excel导出</el-button
    >

    <el-table :data="tableData" row-key="objectId" style="width: 100%">
      <el-table-column label="类目级别" width="180" prop="parentId">
        <template slot-scope="scope">
          {{ scope.row.parentId == "0-0" ? "顶级类目" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="分类名称" width="180" prop="name" />
      <el-table-column label="是否上架" width="180">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isonline"
            :active-value="true"
            :inactive-value="false"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            v-access="['超级管理员']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { categoryGet } from "@/api/job";
import { excelExport2 } from "@/utils/excel-sdk.js";
export default {
  data() {
    return {
      tableData: [
        // {
        //   name: "开发",
        //   parentId: "xx",
        //   isonline: true,
        //   icon: "img.png",
        // },
      ],
    };
  },
  computed: {
    roleName() {
      return this.$store.state.user.userInfo.roleData;
    },
  },
  created() {
    categoryGet(true).then((res) => {
      let { results } = res.data;
      //把定级类目过滤出来
      let parentArr = results.filter((item) => item.parentId == "0-0");
      // 遍历所有主类目，并从results找到主类目children
      parentArr.forEach((parent) => {
        let children = results.filter(
          (child) => child.parentId == parent.objectId
        );
        parent.children = children;
      });
      console.log(parentArr);
      this.tableData = parentArr;
    });
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleOutput() {
      excelExport2(
        this.tableData,
        {
          name: "分类名称",
          parentId: "父级ID",
        },
        "岗位分类数据"
      );
    },
  },
};
</script>
